package view.room
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.ActionEvent;
	import model.Application;
	import model.Character;
	
	import util.Button;
	
	import view.character.CharacterInfoBar;
	import view.toolbar.Dice;
	import view.toolbar.DiceShake;
	import view.toolbar.alert.AlertGameResult;
	import view.toolbar.alert.AlertPlayerOption;
	import view.toolbar.alert.AlertPlayerResult;
	import view.toolbar.alert.AlertWaiting;
	
	public class Room extends Sprite
	{
		private var _character1:CharacterInfoBar;
		private var _character2:CharacterInfoBar;
		
		private var _dicesSelfContainer:Sprite;
		private var _dice1:Dice;
		private var _dice2:Dice;
		private var _dice3:Dice;
		private var _dice4:Dice;
		private var _dice5:Dice;
		
		private var _shake:DiceShake;
		
		private var _opponentContainer:Sprite;
		private var _opponent1:Dice;
		private var _opponent2:Dice;
		private var _opponent3:Dice;
		private var _opponent4:Dice;
		private var _opponent5:Dice;
		
		private var _submitBtn:Button;
		private var _boastBtn:Button;
		
		private var _boastWin:AlertPlayerOption;
		private var _boastResult:AlertPlayerResult;
		private var _gameResult:AlertGameResult;
		private var _waitWin:AlertWaiting;
		
		private static var _instance:Room;
		
		
		public function Room(){
			_instance = this;
			
			createBackgroud();
			createCharacters();
			
			registerEventListener();
		}
		
		
		public static function instance():Room{
			return _instance;
		}
		
		private function registerEventListener():void
		{
			Application.instance().addEventListener(ActionEvent.EVENT_NAME, onAppGetUsersInfo);
		}
		
		private function onAppGetUsersInfo(e:ActionEvent):void
		{
			if (e.stateName&Application.STATE_WAITING || e.stateName&Application.STATE_READY_OTHER){
				createButton();
			}
			
			if (e.stateName&Application.STATE_READY_ME){
				hideStartButton();
				UtbGame.instance.showInfo("等待对家准备开始");
			}
			
			if (e.stateName&Application.STATE_SHOW_USER_INFO){
				showUserInfo();
				createDices();
				createButton();
			}
			
			if (e.stateName&Application.STATE_START){
				
				showShake();
				
				UtbGame.instance.hideInfo();
			}
			
			if (e.stateName&Application.STATE_WAIT_RES_OTHER){
				hideBoastWin();
				
			}
			
			if (e.stateName&Application.STATE_SHOW_OTHER){
				showOtherBoast();
				hideWaitingWin();
			}
			
			if (e.stateName&Application.STATE_SHOW_RESULT){
				showGameResult();
				hideWaitingWin();
				hideBoastButton();
			}
		}
		
		private function showShake():void
		{
			if (!_shake) {
				_shake = new DiceShake;
				_shake.addEventListener(Event.COMPLETE, onShaked);
				
				_shake.x = 340;
				_shake.y = 300;
			}		
			addChild(_shake);
			
			_shake.play();
			
			function onShaked(event:Event):void
			{
				removeChild(_shake);
				showDice();
				createBoastButton();
			}
		}
		
		
		private function hideWaitingWin():void
		{
			if(_waitWin && _waitWin.parent) removeChild(_waitWin);
		}
		
		public function showWaitingWin():void
		{
			if(!_waitWin) _waitWin = new AlertWaiting;
			
			addChild(_waitWin);
		}
		
		private function showGameResult():void
		{
			if(!_gameResult) _gameResult = new AlertGameResult;
			addChild(_gameResult);
			
			_gameResult.showInfo(Application.instance().user.result["info"]);
			
			
			showOpponentDices();
		}
		
		private function showOpponentDices():void
		{
			if(!_opponentContainer){
				_opponentContainer = new Sprite;
				_opponent1 = new Dice;
				_opponent2 = new Dice;
				_opponent3 = new Dice;
				_opponent4 = new Dice;
				_opponent5 = new Dice;
			}
			
			var dices:Array = Application.instance().opponent.dices as Array;
			for(var i:int=1; i<=5; i++){
				var dice:Dice = this["_opponent"+i];
				dice.setPoint(dices[i-1]);
				dice.x = (i-1)*50;
				_opponentContainer.addChild(dice);
			}
			
			_opponentContainer.x = 50;
			_opponentContainer.y = 50;
			addChild(_opponentContainer);
		}
		
		private function showOtherBoast():void
		{
			if(!_boastResult) _boastResult = new AlertPlayerResult;
			addChild(_boastResult);
			
			_boastResult.setCurrentDice(Application.instance().opponent.dices);
		}
		
		private function hideBoastWin():void
		{
			if(_boastWin) removeChild(_boastWin);
		}
		
		private function createBoastButton():void
		{
			_boastBtn = new Button(0, "吹牛");
			_boastBtn.x = 700;
			_boastBtn.y = 470;
			_boastBtn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_boastBtn);
			
			function onClick(event:MouseEvent):void{
				Application.instance().ready();
				hideBoastButton();
				showBoastWin();
			}
		}
		
		public function showBoastWin():void
		{
			if(!_boastWin) _boastWin = new AlertPlayerOption;
			addChild(_boastWin);
			
			_boastWin.setCurrentResult(Application.instance().opponent.dices);
			
			if (_boastResult && _boastResult.parent) removeChild(_boastResult);
		}
		
		private function hideBoastButton():void
		{
			if(_boastBtn) _boastBtn.visible = false;
		}
		
		private function createDices():void
		{
			if(!_dicesSelfContainer){
				_dicesSelfContainer = new Sprite;
				_dice1 = new Dice;
				_dice2 = new Dice;
				_dice3 = new Dice;
				_dice4 = new Dice;
				_dice5 = new Dice;
			}
		}
		
		private function showDice():void
		{
			var dices:Array = Application.instance().user.dices as Array;
			for(var i:int=1; i<=5; i++){
				var dice:Dice = this["_dice"+i];
				dice.setPoint(dices[i-1]);
				dice.x = (i-1)*50;
				_dicesSelfContainer.addChild(dice);
			}
			
			_dicesSelfContainer.x = 340;
			_dicesSelfContainer.y = 370;
			addChild(_dicesSelfContainer);
		}
		
		private function hideStartButton():void
		{
			if(_submitBtn) _submitBtn.visible = false;
		}
		
		private function showUserInfo():void{
			setUserInfo(Application.instance().user);
			setOpponent(Application.instance().opponent);
		}
		
		private function createButton():void{
			_submitBtn = new Button(0, "开始");
			_submitBtn.x = 700;
			_submitBtn.y = 470;
			_submitBtn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_submitBtn);
			
			function onClick(event:MouseEvent):void{
				Application.instance().ready();
				hideStartButton();
				
				UtbGame.instance.showInfo("等待对家准备开始");
			}
		}
		
		private function createCharacters():void{
			_character1 = new CharacterInfoBar;
			_character2 = new CharacterInfoBar(1);
			
			_character1.x = 75;
			_character1.y = 418;
			
			_character2.x = 255;
			_character2.y = 80;
			
			addChild(_character1);
			addChild(_character2);
		}
		
		private function createBackgroud():void{
			addChild(new Bitmap(new UIBackground(850, 547)));
		}
		
		public function setUserInfo(info:Character):void{
			_character1.data = info;
		}
		
		public function setOpponent(info:Character):void{
			_character2.data = info;
		}
	}
}